In the last video she worked with rolling such as Six and all of our calculations were based on fixed
size rolling windows.
So for instance when we calculated the simple moving average 50 for the S&P 500 at each point in time
we included the last 50 observations and to our mean calculations.
So the last 50 observations does not necessarily mean the last 50 calendar days.
And as you might know in finance we typically have no trading prices on bank holidays and weekends and
therefore having a fixed size window of 50 means that we are probably looking back more than 50 calendar
days to get the last 50 observations and the last the 50 year trading prices.
So let's have a look in an example and we are still imported the Apple Boeing data frame and let's have
a look at the first seven rows so you may have to stock prices for Apple and Boeing starting with the
thirty first of December 2009 and already here we can see that we have not on each and every calendar
day a trading price.
We have no trading price on the first of January because this is a bank holiday and we also have no
trading prices on the 2nd and 3rd of January because these this Saturday and Sunday and the next trading
price we can observe on the 4th of January and then also here the 9th and the 10th of January.
Also again Saturday and Sunday.
And let's assume that you want to calculate the simple moving average with a window of 3.
Then we can pass uh the integer 3 to the window parameter and we change the mean method.
So let's have a look here.
So it's no surprise that on the first two timestamps we have any invalid use.
And then starting with the third timestamp we actually F on each timestamp the mean stock price for
these three immediately preceding observations.
So he on the 5th of January we have the stock prices on the thirty first the 4th and the 5th and if
year.
The average actually saw pennies simply takes a year.
The last the three observations and obviously this does not mean that these are the last three calendar
days and actually penned US ignores at the time increments between those observations.
So here we are four days.
And here we have only one day.
However we can change this behavior by passing a fixed size time offset rather than a fixed number of
observations through the window parameter.
So let's again have a look at the first seven observations and let's assume that you want to calculate
the simple moving average of the last three calendar days and instead of passing near the end that uh
three to the windows per meter is simply passing a fixed time offset and we can do this as a string.
So here we have uh three days.
So this is actually nothing new
and that our very first glance we can see here that uh we do not get any any end values at the beginning
and therefore it might be a good idea to have a look at the documentation.
So let's have a look here.
So here we have the window perimeter.
And here we can define the size of the moving window.
And this is the number of observations used for calculating these statistics.
And each window will be a fixed size.
So this is here the above example we FCA a fixed sized window of three and now comes our new example.
If it's an offset so here three days then this will be the time period of each window and each window
will be a variable size based on the observations included in the time period.
And that's also if I look at the parameter Min periods.
So this is the minimum number of observations and window required to have a value.
And otherwise the result is an A and for a window that is specified by an offset.
So this is our new example here than men periods will default to 1.
Otherwise that means we have a sick fixed sized window of three.
So otherwise men period will default to the size of the window.
So if you pass the fixed size then the main period is by default.
The same as our window here three and therefore we get here at the very first two time stamps and they
end values and by passing a time offset the mean periods parameter the default value is 1.
So on each of the timestamps here we have at least the one value and the last the three calendar days
and therefore we do not have you any missing values.
And of course we could also change this and pass uh also here maybe a three to the min periods per metre
so let's say this
and let's have a look and if we can see that we have a lot of any end values and intuitively we actually
only get on the weekdays uh Wednesday Thursday and Friday.
Well use here because only on these days are the days themselves and the immediately two preceding days
uh weekdays and this is actually quite uncomfortable because we have fear many missing values and uh
therefore pen associate quite clever by changing the default parameter to a one period.
If we are working here with time offsets so working with a fixed size time offset is an additional alternative
how to work with uh rolling statistics and in particular this makes a lot of sense when we are working
with irregular times series.
So we are finishing with this video and I hope to see you also in the next one by.